package com.newenergy.wisehome.ui.fragment;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.newenergy.wisehome.R;
import com.xys.libzxing.zxing.encoding.EncodingUtils;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.ISupportFragment;

/**
 *
 */
public class QRCodeFragment extends BaseFragment {

    private static String ARG_QRCONTENT="arg_qrcontent";

    @BindView(R.id.btn_back)
    Button mBtnBack;
    @BindView(R.id.tv_back)
    TextView mTvBack;
    @BindView(R.id.tv_page)
    TextView mTvPage;
    @BindView(R.id.iv_qrcode)
    ImageView mIvQrcode;

    private String mQrContent;

    public QRCodeFragment() {
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_qrcode;
    }

    public static ISupportFragment newInstance(String qrContent) {
        QRCodeFragment fragment = new QRCodeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_QRCONTENT, qrContent);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void init(View rootView) {
        mTvPage.setText(getString(R.string.share_QR_code));
        Bundle args = getArguments();
        if (args != null) {
            mQrContent = args.getString(ARG_QRCONTENT);
            createCode(mQrContent);
        }
    }


    /**
     * 点击生成按钮，根据字符串生成对应的二维码
     *
     * @param qrContent
     */
    public void createCode(String qrContent) {
//        startActivityForResult(new Intent(getActivity(),
//                CaptureActivity.class), 1000);
        int widthPix = dpToPx (300);

        String content = qrContent;
        Bitmap qrBitmap = EncodingUtils.createQRCode(content,
                widthPix, widthPix, null);
        mIvQrcode.setImageBitmap(qrBitmap);

    }


    @OnClick({R.id.btn_back, R.id.tv_back,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();
                break;
            case R.id.tv_back:
                doBack();
                break;
        }
    }


}
